<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class c_main_class extends AWS_CONTROLLER
{	
	public function get_access_rule()
	{
		$rule_action['rule_type'] = "white"; //'black'黑名单,黑名单中的检查  'white'白名单,白名单以外的检查
		$rule_action['actions'] = array();
		
		if (get_setting('guest_explorer') == 'Y')
		{
			$rule_action['actions'][] = 'index';
			$rule_action['actions'][] = 'explore';
		}
		
		return $rule_action;
	}
	
	public function explore_action()
	{		
		//分类
		if (TPL::is_output('block/content_category.tpl.htm','home/explore'))
		{
			$content_category = $this->model('module')->content_category();
			
			TPL::assign('content_category', $content_category);
		}
			
		//问题
		if (TPL::is_output('block/content_question.tpl.htm','home/explore'))
		{
			if ($_GET['category'])
			{
				$topic_count = ZCACHE::get('topic_count_category_' . $_GET['category']);
				
				if ($topic_count === false)
				{
					$topic_count = $this->model("question")->get_topic_category_count($_GET['category']);
					
					ZCACHE::set('topic_count_category_' . $_GET['category'], $topic_count, false, get_setting('cache_level_high'));
				}
				
				TPL::assign('topic_count', $topic_count);
			}
			
			if (!$_GET['sort_type'])
			{
				$_GET['sort_type'] = 'new';
			}
			
			if ($_GET['sort_type'] == 'unresponsive')
			{
				$_GET['answer_count'] = '0';
			}
			
			$question_list = $this->model('question')->get_discuss_list($_GET['page'], get_setting('contents_per_page'), $_GET['sort_type'], $_GET['topic_id'], $this->user_id, $_GET['category'],$_GET['answer_count'], $_GET['day']);
			
			TPL::assign('question_list', $question_list);
			TPL::assign('question_list_bit', TPL::output('question/ajax/list', false));
		}
		
		//边栏菜单
		if (TPL::is_output('block/sidebar_menu.tpl.htm', 'home/explore'))
		{
			TPL::assign('draft_count', $this->model('draft')->get_draft_count('answer', $this->user_id));
			TPL::assign('question_invite_count', $this->model('question')->get_invite_question_list($this->user_id, '', true));
		}	
		
		//边栏最新动态
		if (TPL::is_output('block/sidebar_new_dynamic.tpl.htm','home/explore'))
		{
			$sidebar_new_dynamic = $this->model('module')->sidebar_new_dynamic();
			TPL::assign('sidebar_new_dynamic',$sidebar_new_dynamic);
		
		}
		
		//边栏可能感兴趣的人
		if (TPL::is_output('block/sidebar_recommend_users_topics.tpl.htm','home/explore'))
		{
			$recommend_users_topics = $this->model('module')->recommend_users_topics($this->user_id);
			TPL::assign('sidebar_recommend_users_topics', $recommend_users_topics);
		}
		
		//边栏热门用户
		if (TPL::is_output('block/sidebar_hot_users.tpl.htm','home/explore'))
		{
			$sidebar_hot_users = $this->model('module')->sidebar_hot_user2($this->user_id,$_GET['category']);
			
			TPL::assign('sidebar_hot_users', $sidebar_hot_users);
		}

		//边栏热门话题
		if (TPL::is_output('block/sidebar_hot_topics.tpl.htm','home/explore'))
		{
			$sidebar_hot_topics = $this->model('module')->sidebar_hot_topics($this->user_id,$_GET['category']);
				
			TPL::assign('sidebar_hot_topics', $sidebar_hot_topics);
		}		

		
		if ($_GET['category'] AND $category_info = $this->model('system')->get_category_info($_GET['category']))
		{			
			TPL::assign('category_info', $category_info);
			
			$this->crumb($category_info['title'], '/question/?category=' . $category_info['id']);
			
			$meta_description = $category_info['title'];
			
			if ($category_info['description'])
			{
				$meta_description .=  ' - ' . $category_info['description'];
			}
			
			TPL::set_meta('description', $meta_description);
		}
		
		$this->crumb('发现', '/home/explore/');

		TPL::import_css('css/discussion.css');
		TPL::import_js('js/explore.js');
		
		TPL::output("home/explore");
	}
	
	/**
	 * 热门用户
	 */
	public function users()
	{
		//分类		
		$content_category = $this->model('module')->content_category();
					
		TPL::assign('content_category', $content_category);
		
		if ($_GET['category'] and $category_info = $this->model('system')->get_category_info($_GET['category']))
		{
			TPL::assign('category_info', $category_info);	
		}
		
		$cache_key = "get_hot_users_" . $this->user_id.'_'.$_GET['category'].'_'.calc_page_limit($_GET['page'], get_setting('contents_per_page'));
		
 	$hot_users = $this->model('db_cache')->load($cache_key);
		
  		if(!$hot_users)			
  		{
			$hot_users= $this->model('index')->get_hot_users_V2($this->user_id,$_GET['category'],calc_page_limit($_GET['page'], get_setting('contents_per_page')));
			
			
			if($hot_users)
			{
				foreach($hot_users as $key=>$val)
				{	
					$reputation_topics=null;
					
					$hot_users[$key]['focus'] = $this->model('follow')->user_follow_check($this->user_id, $val['uid']);
					$reputation_topics = $this->model('reputation')->get_reputation_topic($val['uid']);
					if ($reputation_topics)
					{
						$topic_all_count = 0;
							
						foreach ($reputation_topics as $rtkey => $rtval)
						{					
							$rtopic_ids[] = $rtval['topic_id'];
						}
						if($rtopic_row = $this->model('topic')->get_topic_title($rtopic_ids))
						{
							foreach ($rtopic_row as $row_key => $row_val)
							{
								$rtopic[$row_val['topic_id']] = $row_val['topic_title'];
							}
						}
						foreach ($reputation_topics as $rtkey => $rtval)
						{					
							$reputation_topics[$rtkey]['topic_title'] = $rtopic[$rtval['topic_id']];
						}				
						$reputation_topics = array_slice($reputation_topics, 0, 4);
						$hot_users[$key]['reputation_topics']=$reputation_topics;
					}
		
				}
			}
			else
			{
				$hot_users=array();
			}
			
  			$this->model('db_cache')->save($cache_key,$hot_users, 60*60*24);
 		}

		
		foreach($hot_users as $key=>$val)
		{
			$hot_users[$key]['user_info']=$this->model('account')->get_users_by_uid($val['uid'],true);
			$hot_users[$key]['focus'] = $this->model('follow')->user_follow_check($this->user_id, $val['uid']);		
		}
		
		
		$all_users=$this->model('account')->get_user_count(' valid_email=1 AND group_id<>3 ');
		
		$this->model('pagination')->initialize(array(
				'base_url' => get_js_url('home/users/category-' . $_GET['category']),
				'total_rows' => $all_users,
				'per_page' => get_setting('contents_per_page')
		));
		
		TPL::assign('pagination', $this->model('pagination')->create_links());	
		TPL::assign('hot_users', $hot_users);		
		$this->crumb('热门用户', '/home/users/');
		
		TPL::import_css('css/discussion.css');
		TPL::import_js('js/explore.js');		
		TPL::output("home/users");
		
	}
	
	public function index_action()
	{
		if (! $this->user_id)
		{
			HTTP::redirect('/home/explore/guest');
		}

		//边栏分类
		if (TPL::is_output('block/sidebar_category.tpl.htm','home/index'))
		{
			$sidebar_category = $this->model('module')->sidebar_category();
			TPL::assign('sidebar_category', $sidebar_category);
		}

		//边栏菜单
		if (TPL::is_output('block/sidebar_menu.tpl.htm','home/index' ))
		{
			TPL::assign('draft_count', $this->model('draft')->get_draft_count('answer', $this->user_id));
			TPL::assign('question_invite_count', $this->model('question')->get_invite_question_list($this->user_id, '', true));
		}

		//边栏最新动态
		if (TPL::is_output('block/sidebar_new_dynamic.tpl.htm','home/index'))
		{
			$sidebar_new_dynamic = $this->model('module')->sidebar_new_dynamic();
			TPL::assign('sidebar_new_dynamic',$sidebar_new_dynamic);
		}

		//边栏可能感兴趣的人
		if (TPL::is_output('block/sidebar_recommend_users_topics.tpl.htm','home/index'))
		{	
			$recommend_users_topics = $this->model('module')->recommend_users_topics($this->user_id);
			
			TPL::assign('sidebar_recommend_users_topics', $recommend_users_topics);
		}		
		
		//边栏热门用户
		if (TPL::is_output('block/sidebar_hot_users.tpl.htm','home/index'))
		{
			$sidebar_hot_users = $this->model('module')->sidebar_hot_user($this->user_id);
			TPL::assign('sidebar_hot_users', $sidebar_hot_users);
		}
		
		$this->crumb('首页', '/');
		
		TPL::import_css('css/discussion.css');
		
		TPL::import_js(array(
			'js/LocationSelect.js',
			'js/ajaxupload.js',
			'js/index.js'
		));
		
		TPL::output("home/index");
	}
}